package com.sduept.nwld.dataserver.controller.fault;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

import com.esms.common.util.StringUtils;
import com.sduept.bigdata.fault.analysis.log.FaultAnalysisLog;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultAnalysisLogManager;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;

@Named
@ViewScoped
public class FaultReportLogController extends AbstractController {

	private static final long serialVersionUID = 797974837070036813L;


	@Autowired
	private FaultRecordQueryManager frqM;
	
	@Autowired
	private FaultAnalysisLogManager falM;
	
	private List<FaultRecordLogDto> log = new ArrayList<FaultRecordLogDto>();


	@PostConstruct
	private void init() {	
		String faultId = getParameter("faultId");
		if(StringUtils.isNotBlank(faultId)) {
			initLog(faultId);
	  }
	}

	
	
      private void initLog(String faultId) {
    	  FaultRecord fr = frqM.findById(faultId);
    	  List<FaultAnalysisLog> falS = falM.getAnalysisLogByTargetId(faultId);
    	  setLog(getFaultRecordLogDto(fr,falS));
	}


    /**
     * 组织dto
     * @param fr
     * @param falS
     * @return
     */
	private List<FaultRecordLogDto> getFaultRecordLogDto(FaultRecord fr, List<FaultAnalysisLog> falS) {
		// TODO Auto-generated method stub
		List<FaultRecordLogDto> dtos = new ArrayList<>();
		for (FaultAnalysisLog log : falS) {
			FaultRecordLogDto dto = new FaultRecordLogDto();
			dto.setContent(log.getLog());
			dto.setFaultDate(fr.getFaultTime());
			Date date =new Date(log.getVersion());
			dto.setCreateDate(date);
			dtos.add(dto);
		}
		return dtos;
	}

	public List<FaultRecordLogDto> getLog() {
		return log;
	}



	public void setLog(List<FaultRecordLogDto> log) {
		this.log = log;
	}






	/*
       * 故障日志dto
       */
     public class FaultRecordLogDto{
    	 
    	  Date faultDate;
    	 
    	  Date createDate;
    	 
    	  String content;

		public Date getFaultDate() {
			return faultDate;
		}

		public void setFaultDate(Date faultDate) {
			this.faultDate = faultDate;
		}

		public Date getCreateDate() {
			return createDate;
		}

		public void setCreateDate(Date createDate) {
			this.createDate = createDate;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}
    	 
    	 
     }
	
	
	

	
}
